package com.fyb.basic.juc.masterWork;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 测试:https://juejin.cn/post/6996530765152387102
 */
public class MasterTest {

    public static void main(String[] args) throws InterruptedException {
        AtomicLong totalCount = new AtomicLong(0);
        //1、创建Master 并指定工作线程数 将每个work 线程放入到Map中（创建work的同时 启动自己的work线程 从队列中获取任务 然后执行exec 方法）
        //2、启动Master 主线程的同时 遍历map 得到每一个work 线程 从基于数组的队列中获取到对应的任务 然后将此任务给work
        //3、worker得到任务后 调用自己的submit 方法 将任务放到work队列中
        Master<NumAddTask, Long> master = new Master<>(4);

        master.submit(new NumAddTask(1), totalCount::addAndGet);
        master.submit(new NumAddTask(100001), totalCount::addAndGet);
        master.submit(new NumAddTask(200001), totalCount::addAndGet);
        master.submit(new NumAddTask(300001), totalCount::addAndGet);
        master.submit(new NumAddTask(400001), totalCount::addAndGet);
        master.submit(new NumAddTask(500001), totalCount::addAndGet);
        master.submit(new NumAddTask(600001), totalCount::addAndGet);
        master.submit(new NumAddTask(700001), totalCount::addAndGet);
        master.submit(new NumAddTask(800001), totalCount::addAndGet);
        master.submit(new NumAddTask(900001), totalCount::addAndGet);

        Thread.sleep(3000);
        System.out.println("total Count = " + totalCount);

    }
}
